package com.jiyun.security;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiyun.pojo.goods.LoginLog;
import com.jiyun.service.goods.LoginLogService;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

    @Reference
    private LoginLogService loginLogService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 如果登录成功，就进入此方法

         //登录后会调用
        System.out.println("登录成功了，我要在这里记录日志");
        String loginName = authentication.getName();//当前登录用户
        String ip = request.getRemoteAddr();  //获取访问的ip
        LoginLog loginLog=new LoginLog();
        loginLog.setLoginName(loginName);
        loginLog.setLoginTime(new Date()); // 当前登录时间
        loginLog.setIp(ip);
        loginLogService.add(loginLog);

        // 转发，跳转页面
        request.getRequestDispatcher("/main.html").forward(request,response);

    }
}
